package com.hnhczh.alipaydemo.demos.web.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class R<T>{

    @ApiModelProperty(value = "状态")
    private int code;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "错误信息")
    private String error;

    private R(){}

    public R(int code, String error){
        this.code = code;
        this.error = error;
    }

    public static <T> R<T> ok(T data){
        R<T> result = new R<>();
        result.code = 0;
        result.data = data;
        return result;
    }

    public static <T> R<T> fail(String error){
        return fail(-1, error);
    }

    public static <T> R<T> fail(int code, String error){
        R<T> result = new R<>();
        result.code = code;
        result.error = error;
        return result;
    }

    public static <T> R<T> fail(int code, String error,T data){
        R<T> result = new R<>();
        result.code = code;
        result.error = error;
        result.data = data;
        return result;
    }
}
